function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time);
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }

  if ((time + '').length === 10) {
    time = +time * 1000
  }


  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
  let date;
  if (typeof time == 'object') {
    date = time;
  } else {
    date = new Date(parseInt(time));
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1];
    if (result.length > 0 && value < 10) {
      value = '0' + value;
    }
    return value || 0;
  });
  return time_str;
}

export function formatTime(time, option) {
  time = +time * 1000;
  const d = new Date(time);
  const now = Date.now();

  const diff = (now - d) / 1000;

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) { // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
  }
}

export function timer(value){
				if(value==null){value=1484127229}
				return new Date(parseInt(value) * 1000).Format("yyyy-MM-dd hh:mm:ss");    
}

export function mintimer(value){
	      if(value==null){value='暂无'}
				else{
					return new Date(value).getTime().Format("yyyy-MM-dd");    
				}
}

export function decode(value){
	     return decodeURI(value);
}

export function splitOne(value){
	return value.split(',')[0];
}

export function splitTow(value){
	if(value.split(',').length>1){
		return value.split(',')[0]+'...';
	}
	else{
		return value.split(',')[0];
	}
	
}


/* 数字 格式化*/
export function nFormatter(num, digits) {
  const si = [
        { value: 1E18, symbol: 'E' },
        { value: 1E15, symbol: 'P' },
        { value: 1E12, symbol: 'T' },
        { value: 1E9, symbol: 'G' },
        { value: 1E6, symbol: 'M' },
        { value: 1E3, symbol: 'k' }
  ];
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol;
    }
  }
  return num.toString();
}


export function html2Text(val) {
  const div = document.createElement('div');
  div.innerHTML = val;
  return div.textContent || div.innerText;
}


export function toThousandslsFilter(num) {
  return (+num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}

export function cycle(val) {
	if(val==1){
		val="日结"
	}
	else if(val==2){
		val="周结"
	}
	else if(val==3){
		val="双周结"
	}
	else if(val==4){
		val="月结"
	}
	else if(val==5){
		val="无"
	}
	 return val;
}

const isNullOrEmpty = function(val) {
    if (val == null || val == "" || typeof(val) == undefined) {
        return true;
    } else {
        return false;
    }
}

const timeFormat = (value, format) => {
	  var date;
	  if(value){
	  	date = new Date(value.replaceAll('-','/'));
	  	    let y = date.getFullYear();
			    let m = date.getMonth() + 1;
			    let d = date.getDate();
			    let h = date.getHours();
			    let min = date.getMinutes();
			    let s = date.getSeconds();
			    let result = "";
			    if (format == undefined) {
			        result = `${y}-${m < 10 ? "0" + m : m}-${d < 10 ? "0" + d : d} ${
			        h < 10 ? "0" + h : h
			      }:${min < 10 ? "0" + min : min}:${s < 10 ? "0" + s : s}`;
			    }
			    if (format == "yyyy-mm-dd") {
			        result = `${y}-${m < 10 ? "0" + m : m}-${d < 10 ? "0" + d : d}`;
			    }
			    if (format == "yyyy-mm") {
			        result = `${y}-${m < 10 ? "0" + m : m}`;
			    }
			    if (format == "mm-dd") {
			        result = ` ${mm < 10 ? "0" + mm : mm}:${ddmin < 10 ? "0" + dd : dd}`;
			    }
			    if (format == "hh:mm") {
			        result = ` ${h < 10 ? "0" + h : h}:${min < 10 ? "0" + min : min}`;
			    }
			    if (format == "yyyy") {
			        result = `${y}`;
			    }
			  
			    return result;
				  	
	  }
	  else{
	  	return '';
	  }


   
};

 /**
       * @name: handleCutZero
       * @description:  去掉double类型小数点后面多余的0
          参数：old 要处理的字符串或double
          返回值：newStr 没有多余零的小数或字符串
       * @param {*} num
       * @return {*}num
       */
    export  function handleCutZero(num) {
        //拷贝一份 返回去掉零的新串
        let newstr = num;
        //循环变量 小数部分长度
        console.log(num.indexOf('.') - 1);
        let leng = num.length - num.indexOf('.') - 1;
        //判断是否有效数
        if (num.indexOf('.') > -1) {
          //循环小数部分
          for (let i = leng; i > 0; i--) {
            //如果newstr末尾有0
            if (
              newstr.lastIndexOf('0') > -1 &&
              newstr.substr(newstr.length - 1, 1) == 0
            ) {
              let k = newstr.lastIndexOf('0');
              //如果小数点后只有一个0 去掉小数点
              if (newstr.charAt(k - 1) == '.') {
                return newstr.substring(0, k - 1);
              } else {
                //否则 去掉一个0
                newstr = newstr.substring(0, k);
              }
            } else {
              //如果末尾没有0
              return newstr;
            }
          }
        }
        return num;
      }
//    console.log(handleCutZero(res1));// 9950.9213
// 
//  handleCutZero(123.000) -> 123
//  handleCutZero(123.0001) -> 123.0001
//  handleCutZero(10203000.0101000) -> 10203000.0101
//  handleCutZero(10203000) -> 10203000


export {
    isNullOrEmpty,
    timeFormat
}